package com.doradance.app.appactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.doradance.R;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import butterknife.Bind;

public class PersititySignActivity extends BaseActivity {

    @Bind(R.id.et_persitity_sign)
    EditText mEtPersititySign;
    @Bind(R.id.tv_word_number)
    TextView tvWordNumber;
    private int num = 30;
    private String persititysign;
    public final static int MODIFYPERSITITYSIGN = 7;
    public final static int BACKMODIFYPERSITITYSIGN = 8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        persititysign = getIntent().getExtras().getString("persititysign");
        mEtPersititySign.setText(persititysign);
        mEtPersititySign.setSelection(persititysign.length());//设置光标在最后
        tvWordNumber.setText(mEtPersititySign.getText().length()+"/30");
        initToolsBar();
    }

    private void initToolsBar() {
        setTitle(getString(R.string.modify_sign));
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getCustomToolbar().addRightButton("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (TextUtils.isEmpty(mEtPersititySign.getText().toString().trim())) {
                    EasyToast.showToast(getApplicationContext(), "请输入个性签名");
                    return;
                }
                SharePreferenceUtil.saveString(PersititySignActivity.this, "sign", "sign", mEtPersititySign.getText().toString().trim());
                Intent intent = new Intent();
                // 获取用户计算后的结果
                intent.putExtra("signback", mEtPersititySign.getText().toString().trim()); //将计算的值回传回去
                //通过intent对象返回结果，必须要调用一个setResult方法，
                //setResult(resultCode, data);第一个参数表示结果返回码，一般只要大于1就可以，但是
                setResult(BACKMODIFYPERSITITYSIGN, intent);
                finish(); //结束当前的activity的生命周期
            }
        });

        mEtPersititySign.addTextChangedListener(new TextWatcher() {
            private CharSequence wordNum;//记录输入的字数.
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                wordNum = s;//实时记录输入的字数
            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = num - s.length();
                //TextView显示剩余字数
                tvWordNumber.setText(s.length()+"/30");
                selectionStart = mEtPersititySign.getSelectionStart();
                selectionEnd = mEtPersititySign.getSelectionEnd();
                if (wordNum.length() > num) {
                    //删除多余输入的字（不会显示出来）
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    mEtPersititySign.setText(s);
                    mEtPersititySign.setSelection(tempSelection);//设置光标在最后
                }
            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_persitity_sign, null);
    }

    public static void start(Activity activity, String name) {
        Intent intent = new Intent(activity, PersititySignActivity.class);
        intent.putExtra("persititysign", name);
        activity.startActivityForResult(intent, MODIFYPERSITITYSIGN);
    }

}
